/**
 * Copyright (c) 2012-2014 YunZhiSheng(Shanghai) Co.Ltd. All right reserved.
 * @FileName : GaodeRouteActivity.java
 * @ProjectName : vui_assistant
 * @PakageName : cn.yunzhisheng.vui.assistant.gaode
 * @Author : Conquer
 * @CreateDate : 2014-2-24
 */
package cn.yunzhisheng.vui.assistant.gaode;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import cn.yunzhisheng.common.util.LogUtil;
import cn.yunzhisheng.vui.assistant.baidu.BaiduMap;
import cn.yunzhisheng.vui.assistant.baidu.RouteOverlayActivity;
import cn.yunzhisheng.vui.assistant.car.R;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.overlay.BusRouteOverlay;
import com.amap.api.maps2d.overlay.DrivingRouteOverlay;
import com.amap.api.maps2d.overlay.WalkRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.BusRouteQuery;
import com.amap.api.services.route.RouteSearch.DriveRouteQuery;
import com.amap.api.services.route.RouteSearch.FromAndTo;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.RouteSearch.WalkRouteQuery;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;

/**
 * @Module : 隶属模块名
 * @Comments : 描述
 * @Author : Conquer
 * @CreateDate : 2014-2-24
 * @ModifiedBy : Conquer
 * @ModifiedDate: 2014-2-24
 * @Modified:
 * 2014-2-24: 实现基本功能
 */
public class GaodeRouteActivity extends Activity implements LocationSource, OnRouteSearchListener {
	public static final String TAG = "GaodeRouteActivity";
	public static final String TAG_MODE = "TAG_MODE";
	public static final String TAG_FROM_LAT = "TAG_FROM_LAT";
	public static final String TAG_FROM_LNG = "TAG_FROM_LNG";
	public static final String TAG_FROM_CITY = "TAG_FROM_CITY";
	public static final String TAG_FROM_POI = "TAG_FROM_POI";
	public static final String TAG_TO_LAT = "TAG_TO_LAT";
	public static final String TAG_TO_LNG = "TAG_TO_LNG";
	public static final String TAG_TO_CITY = "TAG_TO_CITY";
	public static final String TAG_TO_POI = "TAG_TO_POI";

	private AMap mMap;
	private MapView mMapView;
	private UiSettings mUiSetting;
	private LocationManagerProxy mLocation = null;
	private OnLocationChangedListener mListener;

	private RouteSearch mRouteSearch;
	private AMapLocationListener mAMapLocationListener = new AMapLocationListener() {

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLocationChanged(AMapLocation arg0) {
			if (mListener != null) {
				mListener.onLocationChanged(arg0);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LogUtil.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gaode_route);
		mMapView = (MapView) findViewById(R.id.gaode_route);
		mMapView.onCreate(savedInstanceState);
		if (mMap == null) {
			mMap = mMapView.getMap();
			mUiSetting = mMap.getUiSettings();
		}

		// 路径规划
		mRouteSearch = new RouteSearch(this);
		mRouteSearch.setRouteSearchListener(this);
		mMap.setLocationSource(this);
		// 显示定位按钮
		mUiSetting.setMyLocationButtonEnabled(true);
		// 默认显示定位层
		mMap.setMyLocationEnabled(true);
		computeRoute();
	}

	private void computeRoute() {
		Intent intent = getIntent();
		String model = intent.getStringExtra(RouteOverlayActivity.TAG_MODE);
		double startLat = intent.getDoubleExtra(RouteOverlayActivity.TAG_FROM_LAT, 0);
		double startLng = intent.getDoubleExtra(RouteOverlayActivity.TAG_FROM_LNG, 0);
		String startCity = intent.getStringExtra(RouteOverlayActivity.TAG_FROM_CITY);
		String startPoi = intent.getStringExtra(RouteOverlayActivity.TAG_FROM_POI);
		double endLat = intent.getDoubleExtra(RouteOverlayActivity.TAG_TO_LAT, 0);
		double endLng = intent.getDoubleExtra(RouteOverlayActivity.TAG_TO_LNG, 0);
		String endCity = intent.getStringExtra(RouteOverlayActivity.TAG_TO_CITY);
		String endPoi = intent.getStringExtra(RouteOverlayActivity.TAG_TO_POI);

		Log.d(TAG, "fromLat : " + startLat + " fromLng : " + startLng + " startpoi : " + startPoi + "\n" + "endLat : "
					+ endLat + " endLng : " + endLng + " endpoi : " + endPoi);

		FromAndTo fromAndTo = new FromAndTo(new LatLonPoint(startLat, startLng), new LatLonPoint(endLat, endLng));
		if (BaiduMap.ROUTE_MODE_DRIVING.equals(model)) {
			// 驾车路径规划
			DriveRouteQuery query = new DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null, null, "");
			mRouteSearch.calculateDriveRouteAsyn(query);
		} else if (BaiduMap.ROUTE_MODE_TRANSIT.equals(model)) {
			// 公交路径规划
			BusRouteQuery query = new BusRouteQuery(fromAndTo, RouteSearch.BusLeaseWalk, startCity, 0);
			mRouteSearch.calculateBusRouteAsyn(query);
		} else if (BaiduMap.ROUTE_MODE_WALKING.equals(model)) {
			// 步行路径规划
			WalkRouteQuery query = new WalkRouteQuery(fromAndTo, RouteSearch.WalkDefault);
			mRouteSearch.calculateWalkRouteAsyn(query);
		}

	}

	@Override
	protected void onResume() {
		LogUtil.d(TAG, "onResume");
		super.onResume();
		mMapView.onResume();
	}

	@Override
	protected void onPause() {
		LogUtil.d(TAG, "onPause");
		super.onPause();
		mMapView.onPause();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		LogUtil.d(TAG, "onNewIntent:intent " + intent);
		super.onNewIntent(intent);
		setIntent(intent);
		computeRoute();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		LogUtil.d(TAG, "onSaveInstanceState");
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);
	}

	@Override
	protected void onDestroy() {
		LogUtil.d(TAG, "onDestroy");
		super.onDestroy();
		mMapView.onDestroy();
		mLocation.destory();
		mRouteSearch.setRouteSearchListener(null);
		mRouteSearch = null;
		mAMapLocationListener = null;
	}

	@Override
	public void activate(OnLocationChangedListener listener) {
		mListener = listener;
		mLocation = LocationManagerProxy.getInstance(GaodeRouteActivity.this);
		mLocation.setGpsEnable(true);
		mLocation.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, mAMapLocationListener);
	}

	@Override
	public void deactivate() {
		mLocation.removeUpdates(mAMapLocationListener);
	}

	@Override
	public void onBusRouteSearched(BusRouteResult result, int rCode) {
		LogUtil.d(TAG, "onBusRouteSearched");
		if (rCode == 0) {
			if (result != null && result.getPaths() != null && result.getPaths().size() > 0) {
				BusPath path = (BusPath) result.getPaths().get(0);
				mMap.clear();
				BusRouteOverlay overlay = new BusRouteOverlay(
					GaodeRouteActivity.this,
					mMap,
					path,
					result.getStartPos(),
					result.getTargetPos());
				overlay.removeFromMap();
				overlay.addToMap();
				overlay.zoomToSpan();
			} else {
				Toast.makeText(this, R.string.gaode_nofind_route, 2000).show();
			}
		} else {
			Toast.makeText(this, R.string.gaode_check_net, 2000).show();
		}
	}

	@Override
	public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
		LogUtil.d(TAG, "onDriveRouteSearche");
		if (rCode == 0) {
			if (result != null && result.getPaths() != null && result.getPaths().size() > 0) {
				DrivePath path = (DrivePath) result.getPaths().get(0);
				mMap.clear();
				DrivingRouteOverlay overlay = new DrivingRouteOverlay(
					GaodeRouteActivity.this,
					mMap,
					path,
					result.getStartPos(),
					result.getTargetPos());
				overlay.removeFromMap();
				overlay.addToMap();
				overlay.zoomToSpan();
			} else {
				Toast.makeText(this, R.string.gaode_nofind_route, 2000).show();
			}
		} else {
			Toast.makeText(this, R.string.gaode_check_net, 2000).show();
		}
	}

	@Override
	public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
		LogUtil.d(TAG, "onWalkRouteSearched");
		if (rCode == 0) {
			if (result != null && result.getPaths() != null && result.getPaths().size() > 0) {
				WalkPath path = (WalkPath) result.getPaths().get(0);
				mMap.clear();// 清理之前的图标
				WalkRouteOverlay overlay = new WalkRouteOverlay(
					GaodeRouteActivity.this,
					mMap,
					path,
					result.getStartPos(),
					result.getTargetPos());
				overlay.removeFromMap();
				overlay.addToMap();
				overlay.zoomToSpan();
			} else {
				Toast.makeText(this, R.string.gaode_nofind_route, 2000).show();
			}
		} else {
			Toast.makeText(this, R.string.gaode_check_net, 2000).show();
		}
	}
}
